﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

using SimpleText.Framework;

public partial class LanguageVersioningPage : SimpleTextPage 
{
	protected TextDefinition editTextDef = null;

  protected void Page_Load(object sender, EventArgs e)
  {
		InitPage();

		if (!IsPostBack)
		{
			SaveBtn.Attributes.Add("onclick", "if ( ! validateTranslationsForm()) { return false;	}");
		}

		if (CurrentModel == null)
		{
			Response.Redirect("projects.aspx?msg=noprojectselected");
		}

		//text definitions
		editTextDef = CurrentModel.TextDefinitions[Request.QueryString["name"]];
	}
	
	protected void SaveBtnClicked(object o, EventArgs e)
	{
		editTextDef.Name = Request.Form["Name"];

		foreach (Language lang in CurrentModel.Languages)
		{
			if (editTextDef.Texts.ContainsKey(lang.ShortName))
			{
				editTextDef[lang.ShortName].Content = Request.Form["text_" + lang.ShortName];
			}
			else
			{
				Text newText = new Text();
				newText.Content = Request.Form["text_" + lang.ShortName];
				newText.LanguageShortName = lang.ShortName;
				editTextDef[lang.ShortName] = newText;
			}			
		}

		//write
		SaveModel(CurrentModel, CurrentModel.LoadedFromXmlFilePath);

		//reload model - otherwise next edit will fail
		Session["CurrentModel"] = null;

		Response.Redirect("texts.aspx?msg=success");
	}
}
